package com.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.reggie.common.BaseContext;
import com.reggie.common.R;
import com.reggie.dto.DishDto;
import com.reggie.pojo.ShoppingCart;
import com.reggie.service.IShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private IShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<String> add(@RequestBody ShoppingCart shoppingCart){
        log.info("添加购物车：{}",shoppingCart);
        //id,name,image,user_id,dish_id,setmeal_id,dish_flavor,number,amount,create_time
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        if (dishId != null){//是菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else {//是套餐
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
        }
        //没有此条数据，第一次添加，添加到购物车
        ShoppingCart one = shoppingCartService.getOne(lqw);
        if (one == null){
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(userId);
            shoppingCartService.save(shoppingCart);
        //有此条数据，不是第一次添加，数量+1
        }else {
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
        }

        return R.success("添加购物车成功");
    }

    @GetMapping("/list")
    public R<List<ShoppingCart>> getList(){
        List<ShoppingCart> list = shoppingCartService.list();
        return R.success(list);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart){
        log.info("减少购物车：{}",shoppingCart);
        //id,name,image,user_id,dish_id,setmeal_id,dish_flavor,number,amount,create_time
        Long userId = BaseContext.getCurrentId();

        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId,userId);
        Long dishId = shoppingCart.getDishId();
        Long setmealId = shoppingCart.getSetmealId();
        if (dishId != null){//是菜品
            lqw.eq(ShoppingCart::getDishId,dishId);
        }else {//是套餐
            lqw.eq(ShoppingCart::getSetmealId,setmealId);
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        Integer number = one.getNumber();
        number --;
        one.setNumber(number);
        //还有此条数据，购物车还有，数量-1
        if (number > 0 ){//还有数据 number--
            shoppingCartService.updateById(one);
        //没有此条数据，删除购物车
        }else {//没有数据了 删除此条数据
            shoppingCartService.remove(lqw);
        }
        return R.success(one);
    }

    @DeleteMapping("/clean")
    public R<String> cleanShoppingCart(){
        Long userId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        //表中当前用户的数据清空
        lqw.eq(ShoppingCart::getUserId,userId);
        shoppingCartService.remove(lqw);

        return R.success("清空购物车成功");
    }

}
